package top.swimmer.tools.data.sort;

import java.util.Arrays;

public class QuickSort {
    public static void quickSort(int[] array, int low, int high) {
        if (array == null || array.length <= 1 || low >= high) {
            return;
        }

        int i = low;
        int j = high;
        int index = array[i];
        while (i < j) {
            while (i < j && array[j] > index) {
                j--;
            }
            if (i < j) {
                array[i++] = array[j];
            }
            while (i < j && array[i] < index) {
                i++;
            }
            if (i < j) {
                array[j--] = array[i];
            }
        }
        array[i] = index;
        quickSort(array, low, i - 1);
        quickSort(array, i + 1, high);
    }

    public static void main(String[] args) {
        int[] array = new int[]{8,2,3,4,1,5,1,6,3,9,12,41,4,56,31,52};
        quickSort(array, 0, array.length - 1);
        System.out.println(Arrays.toString(array));
    }
}
